﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace Raider.WinForms.Actions.Linkage
{
    /// <summary>
    /// Base class for action linkers. Defines the methods to link a member of some action, 
    /// specified by <see cref="ActionMember"/> property, with some client component.
    /// </summary>
    public abstract class ActionMemberLinker
    {
        protected ActionMemberLinker(MemberInfo actionMember, IList<Type> clientTypes)
        {
            ActionMember = actionMember;
            ClientTypes = new ReadOnlyCollection<Type>(clientTypes);
        }

        public MemberInfo ActionMember
        {
            get;
            private set;
        }

        public ReadOnlyCollection<Type> ClientTypes
        {
            get;
            private set;
        }

        public bool IsClientSupported(Type clientType)
        {
            return ClientTypes.Any(
                type => type.IsAssignableFrom(clientType));
        }

        public abstract bool LinkMember(IComponent client, IAction action);
        public abstract bool UnLinkMember(IComponent client, IAction action);
    }
}
